package com.a.b.c.d.e.utils.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

import java.sql.Driver;
import java.util.List;

/**
 * Created on 2021/8/25.
 *
 * @author yanshuchun
 */
public class SeleniumUtil {
    private static WebDriver webDriver = null;

    static {
        if (webDriver != null) {
            webDriver.quit();
            webDriver.close();
        }
        System.setProperty("webdriver.chrome.driver", "G:\\file\\dirvers\\chromedriver.exe");
        webDriver = new ChromeDriver();
    }

    public static void main(String[] args) {
        toCleanUpTask();
//        queryMsgFromHomeDoOms("19861956196");
    }

    public static void toCleanUpTask() {
        webDriver.get("http://jira.homedo.com/login.jsp");
        webDriver.findElement(By.id("login-form-username")).sendKeys("yanshuchun");
        webDriver.findElement(By.id("login-form-password")).sendKeys("staR1022");
        webDriver.findElement(By.id("login-form-submit")).click();
        WebElement element = webDriver.findElement(By.cssSelector("#gadget-13296 > div > div.table-container > div > table > tbody"));
        List<WebElement> trs = element.findElements(By.tagName("tr"));
        trs.forEach(item->{
            List<WebElement> ths = item.findElements(By.tagName("th"));
            ths.forEach(th->{
                if("王凡英".equals(th.getText())){
                    List<WebElement> tds = item.findElements(By.tagName("td"));
                    WebElement webElement = tds.get(1);
                    webElement.click();
                    try {
                        Thread.sleep(5000);
                        WebElement tasksElement = webDriver.findElement(By.cssSelector("#content > div.navigator-container > div.navigator-body > div > div > div > div > div > div > div > div.aui-item.list-results-panel > div:nth-child(1) > div > div.list-panel > div.search-results > div > ol"));
                        List<WebElement> imgs = tasksElement.findElements(By.tagName("img"));
                        imgs.forEach(img->{
                            img.click();
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                           dealTask();
                        });
//                        imgs.forEach(img->{
//                            img.click();
//                            try {
//                                Thread.sleep(1000);
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                            WebElement element1 = webDriver.findElement(By.id("action_id_21"));
//                            element1.click();
//                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        });
    }


    public static void dealTask(){
//        WebElement element1 = webDriver.findElement(By.id("action_id_11"));
//        element1.click();
        try {

            Thread.sleep(500);
//            WebElement element2 = webDriver.findElement(By.id("action_id_21"));
//            element2.click();

            WebElement element1 = webDriver.findElement(By.id("opsbar-operations_more"));
            element1.click();
            Thread.sleep(500);
            WebElement element = webDriver.findElement(By.cssSelector("#log-work > span"));
            element.click();

            webDriver.findElement(By.cssSelector("#tt_single_values_orig"));



//            Thread.sleep(500);
//            webDriver.findElement(By.id("opsbar-opsbar-transitions"));

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * fat 环境查找验证码  通过手机号
     *
     * @param phoneNum
     */
    public static void queryMsgFromHomeDoOms(String phoneNum) {
        webDriver.get("http://erp.fat.homedo.com/login?goto=http://oms.fat.homedo.com");
        webDriver.findElement(By.id("txtUserName")).sendKeys("yanshuchun");
        webDriver.findElement(By.id("txtPassword")).sendKeys("staR1022");
        try {
            Thread.sleep(1000);
            WebElement selCompany = webDriver.findElement(By.id("selCompany"));
            Select select = new Select(selCompany);
            select.selectByIndex(1);
            webDriver.findElement(By.id("submit")).click();
            Thread.sleep(500);
//        webDriver.findElement(By.cssSelector("#side-menu > li:nth-child(29)")).click();
//        webDriver.findElement(By.cssSelector("#side-menu > li.active > ul > li > a")).click();
            Thread.sleep(1000);
            webDriver.get("http://config.oms.fat.homedo.com/DataMask/index");
            Thread.sleep(1000);
            webDriver.findElement(By.id("txt_sql")).sendKeys("select Top 10 * from t_Security_Code where toAddress = '" + phoneNum + "' order by id desc");
            Thread.sleep(500);
            webDriver.findElement(By.cssSelector("body > div > div:nth-child(3) > form > button.btn.btn-primary")).click();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
